import {defineStore} from "pinia";
import {ref} from "vue";
import {UserDiscovery} from "../reqApi/UserApi";
import type {ResultInfo} from "../utils/types/ResultInfo";
import type {GameObj} from "../utils/types/Game";
import {LogSuccess} from "../utils/notification/Index";


export const StoreDiscovery = defineStore('StoreDiscovery', () => {

    const gameList = ref<GameObj[]>([])
    const Get = () => {
        if ( gameList.value.length < 0) {
            const item = localStorage.getItem('StoreDiscovery') || "";
            gameList.value = JSON.parse(item) as GameObj[]
        }
        return gameList;
    }
    const Set = (gs: GameObj[]) => gameList.value = gs

    const Clean = () => gameList.value.length = 0

    const Flush = () => {
        gameList.value.length=0
        UserDiscovery({}).then(resp => {
            const result = resp.data as ResultInfo<GameObj[]>
            if (result.success) {
                gameList.value = result.data || []
                localStorage.setItem('StoreDiscovery', JSON.stringify(gameList.value));
            }
            LogSuccess(result.msg)
        });
        localStorage.setItem('StoreDiscovery', JSON.stringify(gameList.value));
    }
    return {gameList, Flush, Get, Clean}

},)
